~ chicken-core (master) /manual/Module (chicken sort)
Trap1[[tags: manual]]2[[toc:]]34== Module (chicken sort)56This module contains several procedures which deal with sorting of7''sequences'' (i.e., lists and vectors).89=== merge1011<procedure>(merge LIST1 LIST2 LESS?)</procedure><br>12<procedure>(merge! LIST1 LIST2 LESS?)</procedure>1314Joins two lists in sorted order. {{merge!}} is the destructive15version of merge. {{LESS? }} should be a procedure of two arguments,16that returns true if the first argument is to be ordered before the17second argument.181920=== sort2122<procedure>(sort SEQUENCE LESS?)</procedure><br>23<procedure>(sort! SEQUENCE LESS?)</procedure>2425Sort {{SEQUENCE}}, which should be a list or a vector. {{sort!}}26is the destructive version of sort.272829=== sorted?3031<procedure>(sorted? SEQUENCE LESS?)</procedure>3233Returns true if the list or vector {{SEQUENCE}} is already sorted.3435=== topological-sort3637<procedure>(topological-sort DAG PRED)</procedure>3839Sorts the directed acyclic graph dag {{DAG}} so that for every edge from vertex40u to v, u will come before v in the resulting list of vertices.4142{{DAG}} is a list of sublists. The car of each sublist is a43vertex. The cdr is the adjacency list of that vertex, i.e. a list of44all vertices to which there exists an edge from the car vertex.45{{pred}} is procedure of two arguments that should compare vertices46for equality.4748Time complexity: O (|V| + |E|)4950<enscript highlight=scheme>51(topological-sort52 '((shirt tie belt)53 (tie jacket)54 (belt jacket)55 (watch)56 (pants shoes belt)57 (undershorts pants shoes)58 (socks shoes))59 eq?)6061=>6263(socks undershorts pants shoes watch shirt belt tie jacket)64</enscript>6566If a cycle is detected during the sorting process, an exception of the67condition kinds {{(exn runtime cycle)}} is thrown.6869---70Previous: [[Module (chicken repl)]]7172Next: [[Module (chicken string)]]